﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RelationshipsPrevalueEditor.cs" company="Vizioz Limited">
//   Open Source - MIT License 2011
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Vizioz.Relationships
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.datatype;
    using umbraco.DataLayer;
    using umbraco.interfaces;

    /// <summary>
    /// The relationships prevalue editor.
    /// </summary>
    public class RelationshipsPrevalueEditor : PlaceHolder, IDataPrevalue
    {
        // referenced datatype
        #region Constants and Fields

        /// <summary>
        /// The _datatype.
        /// </summary>
        private readonly BaseDataType datatype;

        /// <summary>
        /// The _txt limit.
        /// </summary>
        private TextBox txtNumRelationships;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RelationshipsPrevalueEditor"/> class.
        /// </summary>
        /// <param name="dataType">
        /// The data type.
        /// </param>
        public RelationshipsPrevalueEditor(BaseDataType dataType)
        {
            this.datatype = dataType;
            this.SetupChildControls();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets SqlHelper.
        /// </summary>
        public static ISqlHelper SqlHelper
        {
            get
            {
                return Application.SqlHelper;
            }
        }

        /// <summary>
        /// Gets Configuration.
        /// </summary>
        public string Configuration
        {
            get
            {
                var conf =
                    SqlHelper.ExecuteScalar<object>(
                        "select value from cmsDataTypePreValues where datatypenodeid = @datatypenodeid", 
                        SqlHelper.CreateParameter("@datatypenodeid", this.datatype.DataTypeDefinitionId));

                return conf != null ? conf.ToString() : string.Empty;
            }
        }

        /// <summary>
        /// Gets Editor.
        /// </summary>
        public Control Editor
        {
            get
            {
                return this;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The save.
        /// </summary>
        public void Save()
        {
            this.datatype.DBType =
                (DBTypes)Enum.Parse(typeof(DBTypes), umbraco.editorControls.DBTypes.Ntext.ToString(), true);

            string data = this.txtNumRelationships.Text;

            SqlHelper.ExecuteNonQuery(
                "delete from cmsDataTypePreValues where datatypenodeid = @dtdefid", 
                SqlHelper.CreateParameter("@dtdefid", this.datatype.DataTypeDefinitionId));
            SqlHelper.ExecuteNonQuery(
                "insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'')", 
                SqlHelper.CreateParameter("@dtdefid", this.datatype.DataTypeDefinitionId), 
                SqlHelper.CreateParameter("@value", data));
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on load.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!this.Page.IsPostBack)
            {
                this.txtNumRelationships.Text = this.Configuration.Length > 0 ? this.Configuration : "1";
            }
        }

        /// <summary>
        /// The render.
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteLine("<table>");
            writer.Write("<tr><th>Number of Relationships:</th><td>");
            this.txtNumRelationships.RenderControl(writer);
            writer.Write("</td></tr>");
            writer.Write("</table>");
        }

        /// <summary>
        /// The setup child controls.
        /// </summary>
        private void SetupChildControls()
        {
            this.txtNumRelationships = new TextBox { ID = "txtNumRelationships", CssClass = "umbEditorTextField" };

            this.Controls.Add(this.txtNumRelationships);
        }

        #endregion
    }
}